package com.example.orderfood2;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.orderfood2.DatabaseManager.DatabaseManager;
import com.example.orderfood2.Repository.ManagerRepository;
import com.example.orderfood2.Repository.StudentRepository;
import com.example.orderfood2.Repository.TeacherRepository;
import com.example.orderfood2.util.DBUtil;
import com.example.orderfood2.util.ToolsUtil;

public class MainActivity extends AppCompatActivity {
//    private DBUtil dbUtil;
private StudentRepository studentRepository;
    private TeacherRepository teacherRepository;
    private ManagerRepository managerRepository;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 在onCreate中初始化数据库管理器和Repository
        studentRepository = DatabaseManager.getInstance(this).getStudentRepository();
        teacherRepository = DatabaseManager.getInstance(this).getTeacherRepository();
        managerRepository = DatabaseManager.getInstance(this).getManagerRepository();

//        super在这里的作用是调用父类的onCreate(未重写),先执行父类的初始化逻辑，再添加子类特定逻辑。
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
//        创建数据库
//        dbUtil=new DBUtil(this);
    //获取用户框，密码框，三个单选按钮
        EditText username=findViewById(R.id.login_username);
        EditText password=findViewById(R.id.login_password);
        RadioButton manager=findViewById(R.id.login_manager);
        RadioButton teacher=findViewById(R.id.login_teacher);
        RadioButton student=findViewById(R.id.login_student);
        //添加管理员
        Button btn_register_admin=findViewById(R.id.btn_register_admin);
        btn_register_admin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showRegisterAdminDialog();
            }
        });
//    获取登录按钮
        Button login=findViewById(R.id.login_button_login);
//        添加点击监听事件
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //        导入工具类
                String account=ToolsUtil.editTextToString(username);//账号名
                String pwd=ToolsUtil.editTextToString(password);//密码
//        判断账号是否为空，密码是否为空
                if (account.isEmpty()) {
//                    设置弹窗内容，样式并展示 Toast.LENGTH_SHORT表示对应的显示时长大约是 2 秒
                    Toast.makeText(MainActivity.this,"请输入账号",Toast.LENGTH_SHORT).show();
                } else if (pwd.isEmpty()) {
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                }else{
                    //判断单选框
                    if(manager.isChecked()){
                        boolean loginManager=managerRepository.loginManager(account,pwd);
                        if(loginManager){
                            Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                            Intent i=new Intent(MainActivity.this,ManageActivity.class);
//                            将登录的账号传递数据给ManageActivity.class
                            i.putExtra("account",account);
                            startActivity(i);
                        }else {
                            Toast.makeText(MainActivity.this,"管理员账号或密码有误",Toast.LENGTH_SHORT).show();
                        }

                    } else if (teacher.isChecked()) {
                        boolean loginTeacher=teacherRepository.loginTeacher(account,pwd);
                        if(loginTeacher){
                            Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();

                            Intent i=new Intent(MainActivity.this, TeacherActivity.class);
                            i.putExtra("account",account);
                            startActivity(i);
                        }else {
                            Toast.makeText(MainActivity.this,"老师账号或密码有误",Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        boolean loginStudent=studentRepository.loginStudent(account,pwd);
                        if(loginStudent){
                            Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                            Intent i=new Intent(MainActivity.this, StudentActivity.class);
//                            将登录的账号传递数据给ManageActivity.class
                            i.putExtra("account",account);
                            startActivity(i);
                        }else {
                            Toast.makeText(MainActivity.this,"学生账号或密码有误",Toast.LENGTH_SHORT).show();
                        }

                    }
                }
            }
        });
    }
    private void showRegisterAdminDialog() {
        // 创建AlertDialog构建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("注册管理员");

        // 创建包含两个EditText的布局
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(50, 20, 50, 20);

        // 用户名输入框
        final EditText etUsername = new EditText(this);
        etUsername.setHint("用户名");
        etUsername.setInputType(InputType.TYPE_CLASS_TEXT);
        layout.addView(etUsername);

        // 密码输入框
        final EditText etPassword = new EditText(this);
        etPassword.setHint("密码");
        etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        layout.addView(etPassword);

        builder.setView(layout);
//        DBUtil dbUtil=new DBUtil(this);
        // 设置确定按钮
        builder.setPositiveButton("注册", (dialog, which) -> {
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            // 调用注册方法
            long result = managerRepository.registerManager(username, password);
            if (result != -1) {
                Toast.makeText(this, "管理员注册成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "注册失败，用户名可能已存在", Toast.LENGTH_SHORT).show();
            }
        });

        // 设置取消按钮
        builder.setNegativeButton("取消", null);

        // 显示Dialog
        builder.create().show();
    }
}